package com.yanqu.road.web.mgr;

import com.yanqu.road.entity.login.LoginPlayerInfo;
import com.yanqu.road.server.TempMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class LoginPlayerQueueMgr extends TempMgr {

    private static Logger logger = LogManager.getLogger(LoginPlayerQueueMgr.class.getName());

    private static List<LoginPlayerInfo> loginOpenIdList;

    @Override
    public boolean reloadData() {
        loginOpenIdList = new ArrayList<>();
        return true;
    }

    public static int getQueueSize(){
         return loginOpenIdList.size();
    }

    public static int getOpenIndex(String openId){
        synchronized (loginOpenIdList){
            for(int i=0; i<loginOpenIdList.size(); i++){
                if(loginOpenIdList.get(i) != null){
                    if(loginOpenIdList.get(i).getOpenId().equals(openId)){
                        loginOpenIdList.get(i).setLastUpdateTime(System.currentTimeMillis());
                        logger.info("openid:{},index:{}", openId,i+1);
                        return i+1;
                    }
                }
            }
        }
        return -1;
    }

    public static void removeOpenId(String openId){
        synchronized (loginOpenIdList){
            for(int i=0; i< loginOpenIdList.size(); i++ ){
                if(loginOpenIdList.get(i) != null){
                    if(loginOpenIdList.get(i).getOpenId().equals(openId)){
                        loginOpenIdList.remove(i);
                        break;
                    }
                }
            }
        }
    }

    public static void addOpenId(String openId,long currentTime){
        synchronized (loginOpenIdList){
            for(LoginPlayerInfo loginPlayerInfo :loginOpenIdList){
                if(loginPlayerInfo.getOpenId().equals(openId)){
                    loginPlayerInfo.setLastUpdateTime(currentTime);
                    return;
                }
            }
            logger.info("add to queue openId:{}",openId);
            LoginPlayerInfo loginPlayerInfo = new LoginPlayerInfo(openId,currentTime);
            loginOpenIdList.add(loginPlayerInfo);
        }
    }

    public static void checkOverTime(){
        synchronized (loginOpenIdList){
            List<LoginPlayerInfo> removeEntry = new ArrayList<>();
            for(LoginPlayerInfo loginPlayerInfo :loginOpenIdList){
                if(loginPlayerInfo.getLastUpdateTime() + 10 * 1000 < System.currentTimeMillis() ){
                    removeEntry.add(loginPlayerInfo);
                    logger.info("remove to queue openId:{}",loginPlayerInfo.getOpenId());
                }
            }
            loginOpenIdList.removeAll(removeEntry);
        }
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }
}
